<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img src="../../../assets/logo.png"
             alt="" />
      </div>

      <!-- 登录表单区 -->
      <el-form class="login_form"
               ref="loginFormRef"
               :rules="loginFormRules"
               :model="user">
        <!-- 账号输入框 -->
        <el-form-item prop="username">
          <el-input type="text"
                    v-model="user.username"
                    placeholder="请输入账号"
                    prefix-icon="iconfont icon-zhanghao"></el-input>
        </el-form-item>

        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input type="password"
                    v-model="user.password"
                    placeholder="请输入密码"
                    prefix-icon="iconfont icon-mima"></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary"
                     @click="loginBtn()">登录</el-button>
          <el-button type="info"
                     @click="resetLoginForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      user: {
        username: "admin",
        password: "123456"
      },

      //这是表单验证的规则对象
      loginFormRules: {
        username: [
          { require: true, message: "请输入账号", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度需小于3或者大于16个字符",
            trigger: "blur"
          }
        ],
        password: [
          { require: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 22,
            message: "长度需小于8或者大于22个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 登录
    loginBtn () {
      // 在表单ref对象中调用validate(callbakc function(boolean,Object))来进行预验证
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$ajax.post("login", this.user);
        if (res.meta.status !== 200) {
          return this.$message.error("登录失败");
        }
        this.$message.success("登录成功");
        // 登录成功后将token保存到sessionStroage中
        window.sessionStorage.setItem("token", res.data.token);
        // 跳转到home页面
        this.$router.push("/home");
      });
    },
    // 重置按钮
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields(); //获取ref定义的表单实例对象，调用resetFields函数将所有字段值重置为初始值并移除校验结果
    }
  }
};
</script>

<style scoped>
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 6px;
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.avatar_box > img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
}
</style>
